import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
// import HomeView from '../views/HomeView.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    // meta: { title: "数据中心" },
    component: () => import("../views/Home/HomeIndex.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/datacenter",
      },
      {
        path: "/home/datacenter",
        meta: { title: "数据中心/实时数据", requireLogin: true },
        component: () => import("../views/Home/DC/LiveDataIndex.vue"),
      },
      {
        path: "/home/customerdata",
        meta: { title: "数据中心/客户数据", requireLogin: true },
        component: () => import("../views/Home/DC/CustomerDataIndex.vue"),
      },
      {
        path: "/home/order",
        meta: { title: "订单管理/全部订单", requireLogin: true },
        component: () => import("../views/Home/Order/AllOrderIndex.vue"),
      },
      {
        path: "/home/goods",
        meta: { title: "商品管理/全部商品", requireLogin: true },
        component: () => import("../views/Home/Goods/GoodsIndex.vue"),
      },
      {
        path: "/home/addgoods",
        meta: { title: "商品管理/新增商品", requireLogin: true },
        component: () => import("../views/Home/Goods/AddIndex.vue"),
      },
      // 秒杀
      {
        path: "/home/sales",
        meta: { title: "促销管理/秒杀管理", requireLogin: true },
        component: () => import("../views/Home/Sales/SalesIndex.vue"),
      },
      // 新增秒杀
      {
        path: "/home/addsales",
        meta: { title: "促销管理/新增秒杀", requireLogin: true },
        component: () => import("../views/Home/Sales/SecIndex.vue"),
      },
      {
        path: "/home/coupon",
        meta: { title: "促销管理/优惠券管理", requireLogin: true },
        component: () => import("../views/Home/Sales/CouponIndex.vue"),
      },
      {
        path: "/home/addcoupon",
        meta: { title: "促销管理/新增优惠券", requireLogin: true },
        component: () => import("../views/Home/Sales/AddCouponIndex.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login/LoginIndex.vue"),
    meta: { title: "登录" },
  },
  {
    path: "/registry",
    name: "registry",
    component: () => import("../views/Registry/RegistryIndex.vue"),
    meta: { title: "忘记密码" },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

// 全局路由前置守卫 在定义路由实例以后
router.beforeEach((to: any, from, next: any) => {
  // console.log(to); //to表示要去的路由信息 from表示上一个旧的路由信息

  if (to.meta.requireLogin) {
    // 判断是否有token
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }

  document.title = to.meta.title;
});

export default router;
